using System.Linq;
using System.Web.Mvc;
using Dyn.News.Extensions;
using Dyn.News.Models;
using Dyn.News.Services;
using Orchard.ContentManagement;
using Orchard.Core.Feeds;
using Orchard.DisplayManagement;
using Orchard.Localization;
using Orchard.Mvc;
using Orchard.Security;
using Orchard.Themes;
using Orchard;

namespace Dyn.News.Controllers {
    [Themed]
    public class ArticleController : Controller {
        private readonly IOrchardServices _services;
        private readonly ICategoryervice _Categoryervice;
        private readonly IArticleService _ArticleService;
        private readonly IFeedManager _feedManager;

        public ArticleController(
            IOrchardServices services, 
            ICategoryervice Categoryervice, 
            IArticleService ArticleService,
            IFeedManager feedManager,
            IShapeFactory shapeFactory) {
            _services = services;
            _Categoryervice = Categoryervice;
            _ArticleService = ArticleService;
            _feedManager = feedManager;
            T = NullLocalizer.Instance;
            Shape = shapeFactory;
        }

        dynamic Shape { get; set; }
        public Localizer T { get; set; }

        //TODO: (erikpo) Should think about moving the slug parameters and get calls and null checks up into a model binder or action filter
        public ActionResult Item(string categoryPath, string postSlug) {
            //TODO: (erikpo) Move looking up the current category up into a modelbinder
            var CategoryPart = _Categoryervice.Get(categoryPath);
            if (CategoryPart == null)
                return HttpNotFound();

            //TODO: (erikpo) Look up the current user and their permissions to this category post and determine if they should be able to view it or not.
            var postPart = _ArticleService.Get(CategoryPart, postSlug, VersionOptions.Published);
            if (postPart == null)
                return HttpNotFound();

            dynamic model = _services.ContentManager.BuildDisplay(postPart);
            return new ShapeResult(this, model);
        }

        public ActionResult ListByArchive(string categoryPath, string archiveData) {
            //TODO: (erikpo) Move looking up the current category up into a modelbinder
            NewsCategoryPart CategoryPart = _Categoryervice.Get(categoryPath);

            if (CategoryPart == null)
                return HttpNotFound();

            var archive = new ArchiveData(archiveData);

            var list = Shape.List();
            list.AddRange(_ArticleService.Get(CategoryPart, archive).Select(b => _services.ContentManager.BuildDisplay(b, "Summary")));

            _feedManager.Register(CategoryPart);

            dynamic viewModel = Shape.ViewModel()
                .ContentItems(list)
                .Category(CategoryPart)
                .ArchiveData(archive);

            //todo: (heskew) add back
            //.ArchiveData(archive) <-- ??

            // Casting to avoid invalid (under medium trust) reflection over the protected View method and force a static invocation.
            return View((object)viewModel);
        }
    }
}